package com.jjyd.crlp;

import com.jjyd.crlp.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@EnableJpaAuditing
public class CrlpApplication {

	/**
	 * 登陆拦截器
	 */
	@Autowired
	private LoginInterceptor loginInterceptor;


	@Bean
	public WebMvcConfigurer webMvcConfigurer() {
		return new WebMvcConfigurer() {
			/**
			 * 添加拦截器
			 */
			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				registry.addInterceptor(loginInterceptor)
						//排除要执行的url
						.excludePathPatterns("/login")
						.excludePathPatterns("/register")
						.excludePathPatterns("/word/search").excludePathPatterns("/word/random")
						.excludePathPatterns("/tech/addWord").excludePathPatterns("/tech/getNews")
						.excludePathPatterns("/home/getNews")
						//放行错误
						.excludePathPatterns("/error")
						//排除swagger的启动
						.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");

			}
		};
	}

	public static void main(String[] args) {
		SpringApplication.run(CrlpApplication.class, args);
	}

}
